import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_model/home/home_tabs_entity.dart';
import 'package:xvideos/hj_page/community_business/community/source_sub_module_logic.dart';
import 'package:xvideos/hj_page/community_business/widget/source_sections_view.dart';
import 'package:xvideos/hj_page/home_business/home_sub_module/widget/sort_header.dart';
import 'package:xvideos/hj_page/home_business/widget/home_txt_marquee.dart';
import 'package:xvideos/hj_utils/sliver_delegate.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/banner/ads_grid_view_widget.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';

class SourceSubModulePage extends StatelessWidget {
  final ModuleData model;

  const SourceSubModulePage(this.model, {super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BaseSourceModuleLogic>(
      init: instanceController(model),
      global: false,
      builder: (controller) {
        return Stack(
          children: [
            pullYsRefresh(
              onInit: (ctr) => controller.refreshController = ctr,
              onRefresh: (_) => controller.fetchPagedata(),
              onLoading: (_) => controller.fetchPagedata(isRefresh: false),
              child: CustomScrollView(
                // controller: controller.scrollController,
                slivers: [
                  SliverToBoxAdapter(
                    child: AdsGridViewWidget(
                      30,
                      isBigStyle: true,
                      padding: EdgeInsets.fromLTRB(18.w, 30.h, 18.w, 18.h),
                    ),
                  ),
                  SliverToBoxAdapter(
                    child: HomeTxtMarQuee(
                      typeValue: 4,
                      margin: EdgeInsets.fromLTRB(18.w, 0, 18.w, 18.h),
                    ),
                  ),
                  if (controller.showSection)
                    SliverToBoxAdapter(
                      child: SourceSectionView(
                        controller.sections,
                        // moreCick: () => Get.to(AcgMoreTagPage(model), preventDuplicates: false),
                      ),
                    ),
                  if (controller.showSort)
                    SliverPersistentHeader(
                        pinned: true,
                        delegate: MySliverDelegate(
                          maxHeight: 38,
                          minHeight: 38,
                          child: HomeSortHeader(
                            tabs: ['最新更新', '本周最热', '最多观看', '最多购买'],
                            sortCallBack: (sort) {
                              controller.sort = sort + 1;
                              controller.fetchPagedata(isRefresh: true, showLoading: true);
                            },
                          ),
                        )),
                  _buildConten(controller),
                ],
              ),
            ),
            // Positioned(
            //   right: 0,
            //   bottom: 0,
            //   child: PreSaleEntryWidget(),
            // ),
          ],
        );
      },
    );
  }

  Widget _buildConten(BaseSourceModuleLogic controller) {
    if (controller.isLoading)
      return SliverToBoxAdapter(
        child: LoadingCenterWidget(
          height: 400,
        ),
      );
    if (controller.dataSource.isEmpty)
      return SliverToBoxAdapter(
        child: CErrorWidget(),
      );
    switch (controller.pageStyle) {
      case PageStyle.list:
        return SliverList.separated(
            itemCount: controller.dataSource.length,
            itemBuilder: (_, index) {
              // final videoModel = controller.dataSource[index];
              return controller.instanceChild(index);
            },
            separatorBuilder: (_, __) => 14.h.sizeBoxH);
      case PageStyle.waterFlow:
        return SliverPadding(
          padding: EdgeInsets.symmetric(horizontal: 18.w),
          sliver: SliverMasonryGrid.count(
            crossAxisCount: 2,
            mainAxisSpacing: controller.spaceV,
            crossAxisSpacing: controller.spaceH,
            childCount: controller.dataSource.length,
            itemBuilder: (_, index) {
              return controller.instanceChild(index);
            },
          ),
        );
      case PageStyle.grid:
        return SliverPadding(
          padding: EdgeInsets.symmetric(horizontal: 18.w),
          sliver: SliverGrid.builder(
            itemCount: controller.dataSource.length,
            itemBuilder: (_, index) {
              return controller.instanceChild(index);
            },
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: controller.crossAxisCount,
              mainAxisSpacing: controller.spaceV,
              crossAxisSpacing: controller.spaceH,
              childAspectRatio: controller.ratio,
            ),
          ),
        );
      default:
        return SliverToBoxAdapter(
          child: Center(child: Text('')),
        );
    }
  }
}
